<template>
    <div>
        <el-cascader v-bind="$attrs" :options="cascaderOptions" :props="cascaderProps" :key="cascaderKey" />
    </div>
</template>
<script setup lang="ts">
import { getProductTypeSelectList, getProductModelSelectList } from '@/api/manage/resource-management/serial-number-management/index';
import { getFactorySelectList } from '@/api/manage/product-management/product-type/index'
import type { CascaderProps } from 'element-plus'

const cascaderOptions = ref([]);
const cascaderKey = ref(0)
const cascaderProps: CascaderProps = {
    lazy: true,
    lazyLoad(node, resolve) {
        const { level } = node
        if (level == 0) {
            getFactorySelectList().then(res => {
                let nodes = res.result.map((item) => ({
                    value: item.factoryId,
                    label: item.factoryName,
                }))
                resolve(nodes)
            })
            resolve(null)
        } else if (level == 1) {
            getProductTypeSelectList({ factoryId: node.value }).then(res => {
                let nodes = res.result.map((item) => ({
                    value: item.productId,
                    label: item.productName,
                    leaf: true
                }))
                resolve(nodes)
            })
        }
    },
}
</script>
<style lang="scss" scoped></style>